package wicketcool;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import wicketcool.core.Core;
import wicketcool.core.exceptions.ConfigurationFileNotFoundException;
import wicketcool.core.exceptions.PluginDoesNotExistsException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Component("commandLine")
public class DefaultCommandLine implements CommandLine {

    @Autowired
    private Core core;

    public void run(String[] args) {
        try {
            if (args.length > 0) {
                core.executePlugin(args[0], arguments(args));
            } else {
                core.executePlugin("info", arguments(args));
            }
        } catch (PluginDoesNotExistsException e) {
            System.out.println("Plugin " + args[0] + " does not exists");
        } catch (ConfigurationFileNotFoundException e) {
            System.out.println("Cannot perform "
                    + Arrays.asList(args).toString().replace(",", "").replace("[", "'").replace("]", "'") + "! " +
                    "File configuration.properties does not exists.");
        }
    }

    private List<String> arguments(String[] args) {
              
        // return Arrays.asList(Arrays.copyOfRange(args,1,args.length));

        List<String> result = new ArrayList<String>();

        for (int i = 1; i < args.length; i++) {
            result.add(args[i]);
        }
        return result;
    }


}
